package com.zbkj.business.kaoshi.submit;

import com.tiangong.framework.annotation.OpenApi;
import com.tiangong.framework.annotation.OpenApiTag;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.BaseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import java.util.Arrays;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

/**
 * @description: exam_submit佣金申请
 * Author: jason
 * @create: ${creatTime}
 **/
@Service("submit")
@Slf4j
@OpenApi
//@DS("prod")
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
@OpenApiTag(value = "佣金申请",tag = "${projectName}")
public class SubmitService extends BaseService<SubmitDao, Submit> implements IService<Submit> {

    /**
     * list
     */
    @OpenApiTag("查询佣金申请列表")
    public Return list(CDO cdoRequest) {
        cdoRequest.clearBlankValue();
//        cdoRequest.pageHandle();
//        List<CDO> pageList = this.baseMapper.getPageList(cdoRequest);
//        int total = this.baseMapper.getPageListCount(cdoRequest);
//        CDO cdoResult=new CDO().set("total",total).set("datalist",pageList);
        return super.getPageList(cdoRequest);
    }
    /**
     * info
     */
    @OpenApiTag("查询佣金申请信息")
    public Return info(CDO cdoRequest) {
        Submit bo = BeanUtil.toBean(cdoRequest, Submit.class);
        LambdaQueryWrapper<Submit> lqw = buildQueryWrapper(bo);
        Submit submit=super.getOne(lqw);
        return Return.valueOf(submit);
    }

    @OpenApiTag("根据ID查询佣金申请信息")
    public Return queryById(CDO cdoRequest) {
        Submit submit=super.getById(cdoRequest.getLongValue("id"));
        return Return.valueOf(submit);
    }

    private LambdaQueryWrapper<Submit> buildQueryWrapper(Submit submit) {
        LambdaQueryWrapper<Submit> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(Submit::getId);

        lqw.eq(submit.getId() != null, Submit::getId, submit.getId());
        lqw.like(StringUtils.isNotBlank(submit.getSubmitName()), Submit::getSubmitName, submit.getSubmitName());
        lqw.like(StringUtils.isNotBlank(submit.getSubmitPhone()), Submit::getSubmitPhone, submit.getSubmitPhone());
        lqw.like(StringUtils.isNotBlank(submit.getSubmitMoney()), Submit::getSubmitMoney, submit.getSubmitMoney());
        lqw.like(StringUtils.isNotBlank(submit.getSubmitNumber()), Submit::getSubmitNumber, submit.getSubmitNumber());
        lqw.like(StringUtils.isNotBlank(submit.getSubmitUrl()), Submit::getSubmitUrl, submit.getSubmitUrl());
        lqw.like(StringUtils.isNotBlank(submit.getSubmitSign()), Submit::getSubmitSign, submit.getSubmitSign());
        lqw.eq(submit.getSubmitState() != null, Submit::getSubmitState, submit.getSubmitState());
        lqw.like(StringUtils.isNotBlank(submit.getSubmitNotesa()), Submit::getSubmitNotesa, submit.getSubmitNotesa());
        lqw.like(StringUtils.isNotBlank(submit.getSubmitNotesb()), Submit::getSubmitNotesb, submit.getSubmitNotesb());
        lqw.like(StringUtils.isNotBlank(submit.getSubmitNotesc()), Submit::getSubmitNotesc, submit.getSubmitNotesc());
        lqw.like(StringUtils.isNotBlank(submit.getCreatedAt()), Submit::getCreatedAt, submit.getCreatedAt());
        lqw.like(StringUtils.isNotBlank(submit.getUpdatedAt()), Submit::getUpdatedAt, submit.getUpdatedAt());
        return lqw;
    }

    /**
     * save
     */
    @Override
    @OpenApiTag("保存佣金申请信息")
    public Return save(CDO cdoRequest) {
        cdoRequest.remove("createtime");
        cdoRequest.remove("updatetime");
        Submit bo = BeanUtil.toBean(cdoRequest, Submit.class);
        Return ret = this.validEntity(cdoRequest,bo,0);
        if(ret.getCode()!=0){
            return ret;
        }
        super.save(bo);
        return Return.SUCCESS;
    }
    /**
     * update
     */
    @OpenApiTag("更新佣金申请信息")
    public Return update(CDO cdoRequest) {
        cdoRequest.remove("createtime");
        cdoRequest.remove("updatetime");
        Submit bo = BeanUtil.toBean(cdoRequest, Submit.class);
        Return ret = this.validEntity(cdoRequest,bo,1);
        if(ret.getCode()!=0){
            return ret;
        }
        super.updateById(bo);
        return Return.SUCCESS;
    }

    /**
     * delete
     */
    @OpenApiTag("删除佣金申请信息")
    public Return delete(CDO cdoRequest) {
        String[] ids = cdoRequest.getStringValue("ids").split(",");
        boolean b = super.removeBatchByIds(Arrays.asList(ids));
        if(b) {
            return Return.SUCCESS;
        }
        return Return.EXCEPTION;
    }

    private Return validEntity(CDO cdoRequest,Submit entity,Integer type){
        //TODO 做一些数据校验,如唯一约束
        return Return.SUCCESS;
    }
}
